// ignore_for_file: must_be_immutable

import 'dart:math';

import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/widgets/QGButton.dart';
import 'package:junyeyishu/widgets/QGListState.dart';

@immutable
class QGPurchaseNFTListWidget extends StatefulWidget {
  dynamic data;
  Function(String selectedId, String selectedNo) onConfirm;

  QGPurchaseNFTListWidget({super.key, this.data, required this.onConfirm});
  @override
  State<QGPurchaseNFTListWidget> createState() =>
      _QGPurchaseNFTListWidgetState();

  static show(context, data, onConfirm) {
    showDialog(
        useSafeArea: false,
        context: context,
        builder: (context) {
          return QGPurchaseNFTListWidget(data: data, onConfirm: onConfirm);
        });
  }
}

class _QGPurchaseNFTListWidgetState
    extends QGListState<QGPurchaseNFTListWidget> {
  String selectedId = '-1';
  String selectedNo = '-1';

  @override
  void initState() {
    super.initState();

    listURL = url_user_getMyNftListByGoods;
    listKey = "list";

    selectedNo = widget.data?["select_no"];

    onRefresh();
  }

  @override
  Map listParam() {
    return {"pid": widget.data?["pid"], "limit": 20};
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              padding: EdgeInsets.symmetric(horizontal: 15.rpx),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.rpx),
                color: Color(0xFFFFFFFF),
              ),
              child: Column(
                children: [
                  SizedBox(height: 30.rpx),
                  buildText(),
                  buildListContainer(),
                  SizedBox(height: 15.rpx),
                  buildBottom(),
                  SizedBox(height: max(10.rpx, kSafeBottom)),
                ],
              ),
            ),
            buildClose()
          ],
        ),
      ],
    );
  }

  buildText() {
    return Container(
      alignment: Alignment.centerLeft,
      child: Text(
        "请选择1个藏品",
        style: TextStyle(
            fontSize: 16.rpx,
            fontWeight: FontWeight.normal,
            color: Color(0xFF111111)),
      ),
    );
  }

  buildClose() {
    return Positioned(
      right: 0,
      child: GestureDetector(
        onTap: onClose,
        child: Container(
          padding: EdgeInsets.all(12.rpx),
          child: QGImage(asset: "shade_close",width: 15.rpx),
        ),
      ),
    );
  }

  buildListContainer() {
    return SizedBox(
      height: 420.rpx,
      child: Column(
        children: [
          buildList(),
        ],
      ),
    );
  }

  buildBottom() {
    return Container(
      child: Row(
        children: [
          Expanded(
              child: QGButton(
            title: "确定",
            enable: true,
            onTap: onPurchase,
          )),
        ],
      ),
    );
  }

  @override
  itemBuilder(ctx, index, data) {
    return GestureDetector(
        onTap: () {
          toSwitch(data);
        },
        child: buildCell(data));
  }

  buildCell(data) {
    var name = widget.data?["name"];
    if (name is! String || name.isEmpty) {
      name = " ";
    }

    return Container(
      margin: EdgeInsets.only(top: 15.rpx),
      padding: EdgeInsets.symmetric(horizontal: 10.rpx, vertical: 12.rpx),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.rpx),
          border: Border.all(
              width: 0.5.rpx, color: Color.fromRGBO(17, 17, 17, 0.6))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              FittedBox(
                child: Text(
                  name,
                  style: TextStyle(
                      color: Color(0xFF111111),
                      fontSize: 14.rpx,
                      fontWeight: FontWeight.normal),
                ),
              ),
              SizedBox(width: 15.rpx),
              FittedBox(
                child: Text(
                  '#${data?["nft_no"]}/${data?["total"]}',
                  style: TextStyle(
                      color: const Color(0xff111111),
                      fontSize: 14.rpx,
                      fontWeight: FontWeight.normal),
                ),
              ),
            ],
          ),
          selectedNo == data?["nft_no"]
              ? QGImage(asset: "login_check", width: 16.rpx)
              : Container()
        ],
      ),
    );
  }

  onClose() {
    Navigator.of(context).pop();
  }

  toSwitch(data) {
    selectedId = data?["nft_id"];
    selectedNo = data?["nft_no"];
    if (mounted) {
      setState(() {});
    }
  }

  onPurchase() {
    if (selectedNo == '-1') {
      toast("请选择一个藏品");
      return;
    }

    widget.onConfirm(selectedId, selectedNo);
    Navigator.of(context).pop();
  }
}
